package com.tms.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 本地数据源Mybatis配置类
 */
@Configuration
@MapperScan(value = "com.tms.mapper.develop", sqlSessionFactoryRef = "sqlSessionFactoryBeanDevelop")
public class MyBatisConfigDevelop {

    @Autowired
    @Qualifier("dsDevelop")
    DataSource dsDevelop;

    @Bean
    @Primary
    SqlSessionFactory sqlSessionFactoryBeanDevelop() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dsDevelop);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/mappers/develop/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    @Primary
    SqlSessionTemplate sqlSessionTemplateDevelop() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBeanDevelop());
    }
}
